package chu.dejian.db;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;

import java.util.ArrayList;
import java.util.List;

import chu.dejian.entity.SendedRequestEntity;
import chu.dejian.entity.SendedRequestEntity;

public class SendedRequestDao {
	public static final String SENDED_REQUEST_TABLE = "sended_request";

	public static final String KEY_AR_ID = "arid";
	public static final String KEY_ATTENDER = "attender";
	public static final String KEY_ATTENDEE_NAME = "attendee_name";
	public static final String KEY_ATTENDEE_LEVEL = "attendee_level";
	public static final String KEY_STATU = "statu";

	public static final String SENDED_REQUEST_TABLE_CREATE = "CREATE TABLE "
			+ SENDED_REQUEST_TABLE + " ("
			+ KEY_AR_ID + " INTEGER, "
			+ KEY_ATTENDER + " VARCHAR, "
			+ KEY_ATTENDEE_NAME + " VARCHAR, "
			+ KEY_ATTENDEE_LEVEL + " TINYINT, "
			+ KEY_STATU + " TINYINT);";

	public boolean insertRequest(SendedRequestEntity requestEntity) {
		if(getRequest(requestEntity.arId) != null) return updateRequest(requestEntity);
		else return DBHelper.getInstance().getDatabase().insert(SENDED_REQUEST_TABLE, null, createContentValues(requestEntity)) != -1;
	}

	public void insertRequests(List<SendedRequestEntity> requests){
		if(requests == null || requests.size() == 0) return;
		for(SendedRequestEntity sendedRequest : requests){
			insertRequest(sendedRequest);
		}
	}

	public boolean updateRequest(SendedRequestEntity requestEntity) {
		return DBHelper.getInstance().getDatabase().update(SENDED_REQUEST_TABLE, createContentValues(requestEntity), KEY_AR_ID + "=" + requestEntity.arId , null) > 0;
	}

	public boolean deleteRequest(int arid) {
		return DBHelper.getInstance().getDatabase().delete(SENDED_REQUEST_TABLE, KEY_AR_ID + "=" + arid, null) > 0;
	}

	public List<SendedRequestEntity> getAllRequest() {
		Cursor cursor = DBHelper.getInstance().getDatabase().query(SENDED_REQUEST_TABLE,null,	null, null, null, null, null);
		List<SendedRequestEntity> requestList = new ArrayList<>();
		while (cursor.moveToNext()) {
			requestList.add(getRequestByCursor(cursor));
		}
		cursor.close();
		return requestList;
	}

	public SendedRequestEntity getRequestByCursor(Cursor cursor){
		SendedRequestEntity requestEntity = new SendedRequestEntity();
		requestEntity.arId = cursor.getInt(cursor.getColumnIndex(KEY_AR_ID));
		requestEntity.attendeeName = cursor.getString(cursor.getColumnIndex(KEY_ATTENDEE_NAME));
		requestEntity.attendee = cursor.getString(cursor.getColumnIndex(KEY_ATTENDER));
		requestEntity.attendeeLevel = cursor.getInt(cursor.getColumnIndex(KEY_ATTENDEE_LEVEL));
		requestEntity.status = cursor.getInt(cursor.getColumnIndex(KEY_STATU));
		return requestEntity;
	}

	public SendedRequestEntity getRequest(int arid) throws SQLException {
		Cursor cursor = DBHelper.getInstance().getDatabase().rawQuery("SELECT * FROM "+ SENDED_REQUEST_TABLE + " WHERE "+ KEY_AR_ID +" = "+ arid,null);
		if (cursor != null && cursor.moveToFirst()) {
			return getRequestByCursor(cursor);
		}
		return null;
	}

	private ContentValues createContentValues(SendedRequestEntity requestEntity) {
		ContentValues values = new ContentValues();
		values.put(KEY_AR_ID,requestEntity.arId);
		values.put(KEY_ATTENDER,requestEntity.attendee);
		values.put(KEY_ATTENDEE_NAME,requestEntity.attendeeName);
		values.put(KEY_ATTENDEE_LEVEL,requestEntity.attendeeLevel);
		values.put(KEY_STATU,requestEntity.status);
		return values;
	}
}
